package com.example.app2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.app2.Template.StandardReceiver;

public class BroadcastActivity extends AppCompatActivity implements View.OnClickListener {

    private static String phone ="123456";
    private static String pasword ="123456";

    private static final String STANDARD_ACTION = "LLKLKL";

    private Button id;
    private EditText textphone;
    private EditText password;
    private StandardReceiver standardReceiver;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast);
        //登录按钮
        id = findViewById(R.id.login);
        //获取账号
        textphone = findViewById(R.id.textphone);
        //获取密码
        password = findViewById(R.id.password);
        //点击登录按钮触发点击事件
        id.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        //先判断账号和密码是否正确
        if (phone.equals(textphone.getText().toString()) && pasword.equals(password.getText().toString())) {

            //登录成功之后进行页面的跳转和发送广播,下面是标准广播
            Intent intent = new Intent(STANDARD_ACTION);
            intent.putExtra("a", "aa");

            //发送广播
            sendBroadcast(intent);
            //跳转页面
            intent.setClass(getApplicationContext(), BroadcastActivity1.class);


            //开启跳转页面
            startActivity(intent);


            Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        standardReceiver = new StandardReceiver();
        // 创建一个意图过滤器，只处理STANDARD_ACTION的广播
        IntentFilter intentFilter = new IntentFilter(STANDARD_ACTION);
        //注册一个接收器，注册之后才能正常接受广播
        registerReceiver(standardReceiver, intentFilter);

    }

    @Override
    protected void onStop() {
        super.onStop();
        //注销接收器，注销之后不再接收广播
        unregisterReceiver(standardReceiver);


    }
}